package cn.qfei.qiyuesuo.service.impl;

import cn.qfei.qiyuesuo.bo.*;
import cn.qfei.qiyuesuo.common.TenantTypeConstant;
import cn.qfei.qiyuesuo.common.enums.ErrorCode;
import cn.qfei.qiyuesuo.service.KaixuanSignService;
import cn.qfei.qiyuesuo.vo.FeishuContractNoticeVo;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class KaixuanSignServiceImpl implements KaixuanSignService {
    @Autowired
 private  QiyuesuoApiServiceImpl qiyuesuoApiServiceImpl;
//    @Override
//    public void updateSignSatus(FeishuContractNoticeVo vo) {
//        DetailContractBo dto=qiyuesuoApiServiceImpl.getContractDetail(vo.getContractId(),vo.getQfFileId());
//        if (dto==null||"SIGNING".equals(dto.getStatus())){//非签约中直接返回
//          return;
//        }
//        //处理返送用户签约链接
////       int snndSignFlag= sendUserSigin(vo.getContractId(),vo.getQfFileId());
//        int snndEnterpriseFlag= snndEnterpriseAuthorization(vo.getContractId(),vo.getQfFileId());

//    }



    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("1234566");
        list.add("222333");
        String s= JSON.toJSONString(list);
        System.out.println(s);
        System.out.println("[123123123213,123213213213]".equals(s));

        List<Long> list1=new ArrayList<>();
        list1.add(123123123213l);
        list1.add(123213213213l);
        String s1= JSON.toJSONString(list1);
        System.out.println(s1);
        System.out.println("[123123123213,123213213213]".equals(s1));




    }
    /**签合同章
     *
     * @param contractId
     * @return
     */
    @Override
    public ErrorCode signContractBycompany(String contractId,String ourPartyName,String sealId) {

        DetailContractBo dto=qiyuesuoApiServiceImpl.getContractDetail(contractId);
//        if (dto==null||"SIGNING".equals(dto.getStatus())){//非签约中直接返回
//            log.info("合同状态不对直接返回contractId={}",contractId);
//            return ErrorCode.CONTRACT_SATUS_FA;
//        }

        SignNeedParemBo signNeedParemBo=getSignNeedParemBo(dto);
        if (signNeedParemBo==null){
            log.info("合同状态不对直接返回contractId={}",contractId);
            return ErrorCode.CONTRACT_SATUS_FA;
        }
        String companyId=signNeedParemBo.getCompanyId();
//        String sealIds=signNeedParemBo.getSealIds();
//        sealIds="3207614835341980195";
//         * @param sealIds  印章id  可传入多个，格式：[123123123213,123213213213]
//     * @param companyId   法人单位id
//     * @param contractId  电子签约文件id
        //处理返送用户签约链接
        String succ=qiyuesuoApiServiceImpl.signContractBycompany(contractId,ourPartyName,sealId);
        log.info("签章返回{}",succ);
        return ErrorCode.SUCCESS;
    }

    /**
     *创建合同
     * @param contractId
     * @param ourPartyName
     * @return
     */
    @Override
    public ErrorCode addContract(String contractId, String ourPartyName) {


        return null;
    }


    private SignNeedParemBo getSignNeedParemBo(DetailContractBo dto) {
        SignNeedParemBo signNeedParemBo=new SignNeedParemBo();
        List<DetailSignatoryBo> signatories=dto.getSignatories();
        //CORPORATE（平台法人单位），INNER_COMPANY（内部法人单位），COMPANY（外部法人单位），PERSONAL（个人）
        List<Long> list=new ArrayList<>();

        if (signatories==null||signatories.size()==0){
            return null;
        }
        for (DetailSignatoryBo bo:signatories) {
            if (bo!=null){
                String tenantType=bo.getTenantType();
                if (TenantTypeConstant.INNER_COMPANY.equals(tenantType)||TenantTypeConstant.CORPORATE.equals(tenantType)){
                    signNeedParemBo.setCompanyId(bo.getTenantId());
                    List<SealStatsInfoBo> sealStatsInfos=bo.getSealStatsInfos();
                    if (sealStatsInfos==null||sealStatsInfos.size()==0){
                        continue;
                    }
                    List<Long> sealIdList=handleSealIds(sealStatsInfos);
                    if (sealIdList!=null&&sealIdList.size()>0){
                        list.addAll(sealIdList);
                    }
                }
            }
        }
        if (list.size()>1){
            signNeedParemBo.setSealIds(JSON.toJSONString(list));
        }else if (list.size()==1){
            signNeedParemBo.setSealIds(String.valueOf(list.get(0)));
        }
        return signNeedParemBo;

    }

    private List<Long> handleSealIds(List<SealStatsInfoBo> sealStatsInfos) {
        List<Long> list=new ArrayList<>();
        if (sealStatsInfos==null||sealStatsInfos.size()==0){
            return null;
        }
        for (SealStatsInfoBo bo: sealStatsInfos ) {
            if (bo==null){
                continue;
            }
            String sealId=bo.getSealId();
            if (StringUtils.isNotBlank(sealId)){
                list.add(Long.valueOf(sealId));
            }
        }
            return list;
    }

    /**
     * 通知企业授权
     * @param contractId
     * @param qfFileId
     * @return
     */
//    private int snndEnterpriseAuthorization(long contractId, String qfFileId) {
//        CompanysignsilentUrlBo dto=qiyuesuoApiServiceImpl.getCompanysignsilentUrl(contractId,qfFileId);
//
//        CompanysignsilentUrlBo dto1=qiyuesuoApiServiceImpl.getCompanysignsilentUrl(contractId,qfFileId);
//        if (dto==null|| StringUtils.isBlank(dto.getUrl())){
//            return 0;
//        }
//        //通知用户签约
//        String url=dto.getUrl();
//        return 1;
//
//    }

    /**
     * 处理返送用户签约链接
     * @param contractId
     * @return
     */
    public String getPreSignurl(String contractId) {
        PresignurlBo dto=qiyuesuoApiServiceImpl.getContractPresignurl(contractId);
        if (dto==null|| StringUtils.isBlank(dto.getPresignUrl())){
            return null;
        }
        //通知用户签约
        String presignUrl=dto.getPresignUrl();
        return presignUrl;
    }
}
